#!/bin/bash

#****************
# Building tools
#****************
mkdir -p out
#gcc -o out/acceleration -lm tools/acceleration/acceleration.c || exit
#out/acceleration 0.07 400 0.3 1 > libs/avr/Stepper/AccelerationTable.h

#***************************************
# Searching for source and header files
#***************************************
LIB_AVR_SRC=`find libs/avr -type f -name *.cpp`
LIB_AVR_INC=`find libs/avr -type d -printf "-I%p "`
LIB_COMMON_SRC=`find libs/common -type f -name *.cpp`
LIB_COMMON_INC=`find libs/common -type d -printf "-I%p "`
LIB_LINUX_SRC=`find libs/linux -type f -name *.cpp`
LIB_LINUX_INC=`find libs/linux -type d -printf "-I%p "`
ROBOTS_CONFIG=`find robots -name *.cfg`
TOOLS_CONFIG=`find tools -name *.cfg`

#********************
# Building all tools
#********************
for config in $TOOLS_CONFIG; do

	TOOL=`basename $config .cfg`
	ROOT=`dirname $config`
	INC="`find $ROOT -type d -printf "-I%p "` $LIB_COMMON_INC $LIB_LINUX_INC"
	SRC="`find $ROOT -type f -name *.cpp` $LIB_COMMON_SRC $LIB_LINUX_SRC"

	echo "Building $TOOL..."

	#***************************************
	# Load tool build options ($CC and $LD)
	#***************************************
	. $config

	#**************************
	# Prepare output directory
	#**************************
	OUT="out/$TOOL"
	mkdir -p $OUT || exit

	#*************
	# Compilation
	#*************
	OBJS=""
	for src in $SRC; do
		obj="$OUT/`basename $src .cpp`.o"
		$CC -o $OUT/`basename $src .cpp`.o $INC $src || exit
		OBJS="$OBJS $obj"
	done

	#***********************
	# Link and clean object
	#***********************
	$LD -o $OUT/$TOOL $OBJS || exit
	rm $OUT/*.o

done

#*********************
# Building all robots
#*********************
for config in $ROBOTS_CONFIG; do

	ROBOT=`basename $config .cfg`
	ROOT=`dirname $config`
	INC="`find $ROOT -type d -printf "-I%p "` $LIB_AVR_INC $LIB_COMMON_INC"
	SRC="`find $ROOT -type f -name *.cpp` $LIB_AVR_SRC $LIB_COMMON_SRC"

	echo "Building $ROBOT..."

	#****************************************
	# Load robot build options ($CC and $LD)
	#****************************************
	. $config

	#**************************
	# Prepare output directory
	#**************************
	OUT="out/$ROBOT"
	mkdir -p $OUT || exit

	#*************
	# Compilation
	#*************
	OBJS=""
	for src in $SRC; do
		obj="$OUT/`basename $src .cpp`.o"
		#$CC -S -o $OUT/`basename $src .cpp`.s $INC $src || exit
		$CC -o $OUT/`basename $src .cpp`.o $INC $src || exit
		OBJS="$OBJS $obj"
	done

	#***********************
	# Link and clean object
	#***********************
	$LD -o $OUT/$ROBOT.out $OBJS || exit
	rm $OUT/*.o
	avr-objcopy -O ihex -R .eeprom $OUT/$ROBOT.out $OUT/$ROBOT.hex || exit

	#************************
	# Generate debug listing
	#************************
	if [ "$1" = "-g" ]; then
		avr-objdump -Sd $OUT/$ROBOT.out > $OUT/$ROBOT.lst || exit
	elif [ -e $OUT/$ROBOT.lst ]; then
		rm $OUT/$ROBOT.lst
	fi

done

#**************
# Generate doc
#**************
#echo "Building documentation..."
#doxygen

grep __heap_start Eve.map
echo "Done."
